package com.xiaozhen.blog.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaozhen.blog.entity.Blog;
import com.xiaozhen.blog.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @create 2023-04-19 14:56
 * 博客首页
 */
@Controller
@RequestMapping("/")
@Slf4j
public class IndexController {
    @Autowired
    private BlogService blogService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private TagService tagService;

    @Autowired
    private LikeService likeService;
    /**
     * 小圳博客首页
     *
     * @param page  第几页
     * @param model
     * @return
     */
    @RequestMapping("/")
    public String index(@RequestParam(value = "page", required = false) Long page,
                        Model model) {
        // 如果没有传入参数，那么我们把page定义为第 1 页
        if (page == null) {
            page = 1L;
        }
        // 创建对象，传递分页相关参数，每页记录数为 6
        Page<Blog> pageParam = new Page<>(page, 6);

        // 点赞次数


        model.addAttribute("page", blogService.blogPage(pageParam));
        model.addAttribute("types", typeService.typeListbByCount());
        model.addAttribute("tags", tagService.tagListbByCount());
        model.addAttribute("blogs", blogService.newBlog());
        return "index";
    }

    /**
     * 根据博客id获取博客详情
     *
     * @param id
     * @param model
     * @return
     */
//    @PreAuthorize("hasAuthority('adminzz')")
    @RequestMapping("/blog/{id}")
    public String blog(@PathVariable Long id, Model model) {
        model.addAttribute("blog", blogService.getBlogAndUser(id));
        long entityLikeCount = likeService.findEntityLikeCount(1, id);
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        int entityLikeStatus;
        if (authentication.getName() == "anonymousUser"){
            entityLikeStatus = 0;
        }else {
            Long userId = Long.parseLong(authentication.getName());
            entityLikeStatus = likeService.findEntityLikeStatus(userId, 1, id);
        }
        model.addAttribute("entityLikeCount",entityLikeCount);
        model.addAttribute("entityLikeStatus",entityLikeStatus);
        return "blog";
    }

    /**
     * 通过搜索查询博客列表
     * @param page
     * @param query
     * @param model
     * @return
     */
    @GetMapping("/search")
    public String search(@RequestParam(value = "page", required = false) Long page,
                         @RequestParam String query, Model model) {
        // 如果没有传入参数，那么我们把page定义为第 1 页
        if (page == null) {
            page = 1L;
        }
        // 创建对象，传递分页相关参数，每页记录数为 6
        Page<Blog> pageParam = new Page<>(page, 6);

        model.addAttribute("page", blogService.search(query, pageParam));
        model.addAttribute("query", query);
        return "search";
    }

    
}
